<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="食堂名称" name="canteenName">
						<a-input v-model:value="searchFormState.canteenName" placeholder="请输入食堂名称" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizCanteenAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-button
						v-if="hasPerm('bizCanteenBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchBizCanteen"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'canteenPriceModifyFlag'">
					{{ $TOOL.dictTypeData('COMMON_SWITCH', record.canteenPriceModifyFlag) }}
				</template>
				<template v-if="column.dataIndex === 'extJson'">
					{{ record.extJsonObject.directorName }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="handleQrcode(record)">二维码</a>
						<a-divider type="vertical" />
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizCanteenEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizCanteenEdit', 'bizCanteenDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizCanteen(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizCanteenDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<a-modal v-model:open="qrcode_of" @open="handleOk">
		<div class="modal">{{ openParams.canteenName }}</div>
		<div class="modal">
			<canvas class="" ref="qrcodeContainer"></canvas>
		</div>
	</a-modal>
</template>

<script setup name="canteen">
	// import tool from '@/utils/tool'
	import { cloneDeep, isEmpty } from 'lodash-es'
	import Form from './form.vue'
	import bizCanteenApi from '@/api/biz/bizCanteenApi'
	// import { Empty } from 'ant-design-vue'
	// import bizOrgApi from '@/api/biz/bizOrgApi'
	import QRCode from 'qrcode'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	// const treeData = ref([])
	// const cardLoading = ref(true)
	const qrcode_of = ref(false)
	const qrcodeContainer = ref()
	// 替换treeNode 中 title,key,children
	// const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 默认展开的节点
	// const defaultExpandedKeys = ref([])
	const columns = [
		// {
		// 	title: '组织ID',
		// 	dataIndex: 'orgId'
		// },
		{
			title: '食堂名称',
			dataIndex: 'canteenName'
		},
		{
			title: '食堂午餐价格',
			dataIndex: 'canteenLunchPrice'
		},
		{
			title: '食堂晚餐价格',
			dataIndex: 'canteenDinnerPrice'
		},
		{
			title: '食堂价格是否可修改',
			dataIndex: 'canteenPriceModifyFlag'
		},
		{
			title: '午餐开始时间',
			dataIndex: 'lunchBeginTime'
		},
		{
			title: '午餐结束时间',
			dataIndex: 'lunchEndTime'
		},
		{
			title: '晚餐开始时间',
			dataIndex: 'dinerBeginTime'
		},
		{
			title: '晚餐结束时间',
			dataIndex: 'dinerEndTime'
		},
		{
			title: '审批通知人',
			dataIndex: 'extJson'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizCanteenEdit', 'bizCanteenDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 250
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		// loadTreeData()
		const searchFormParam = cloneDeep(searchFormState.value)
		return bizCanteenApi.bizCanteenPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizCanteen = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizCanteenApi.bizCanteenDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizCanteen = (params) => {
		bizCanteenApi.bizCanteenDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	// 加载左侧的树
	// const loadTreeData = () => {
	// 	bizOrgApi
	// 		.orgTree()
	// 		.then((res) => {
	// 			cardLoading.value = false
	// 			if (res !== null) {
	// 				treeData.value = res
	// 				if (isEmpty(defaultExpandedKeys.value)) {
	// 					// 默认展开2级
	// 					treeData.value.forEach((item) => {
	// 						// 因为0的顶级
	// 						if (item.parentId === '0') {
	// 							defaultExpandedKeys.value.push(item.id)
	// 							// 取到下级ID
	// 							if (item.children) {
	// 								item.children.forEach((items) => {
	// 									defaultExpandedKeys.value.push(items.id)
	// 								})
	// 							}
	// 						}
	// 					})
	// 				}
	// 			}
	// 		})
	// 		.finally(() => {
	// 			cardLoading.value = false
	// 		})
	// }
	// 点击树查询
	// const treeSelect = (selectedKeys) => {
	// 	if (selectedKeys.length > 0) {
	// 		searchFormState.value.orgId = selectedKeys.toString()
	// 	} else {
	// 		delete searchFormState.value.orgId
	// 	}
	// 	tableRef.value.refresh(true)
	// }
	const openParams = ref({})
	function handleQrcode(params) {
		qrcode_of.value = true
		openParams.value = params
		handleOk()
	}
	function handleOk() {
		nextTick(() => {
			QRCode.toCanvas(qrcodeContainer.value, openParams.value.id, { width: 300 }, function (error) {
				if (error) {
					console.error(error, '二维码生成失败！')
				}
			})
		})
	}
</script>

<style lang="less" scoped>
	.modal {
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
